package com.adcc.swingui.conf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.adcc.swingui.bean.ToolBean;
import com.adcc.swingui.bean.ToolComponentBean;

/**
 * 工具栏配置
 * 
 * @author charlie
 * 
 */
public class ToolBarConf {
	private static Logger logger = Logger.getLogger(ToolBarConf.class);
	private static Map<String, ToolBean> toolConf = new HashMap<String, ToolBean>();

	public static void initConf(String path) {
		try {
			SAXReader reader = new SAXReader();
			Document doc = reader.read(path);
			Element root = (Element) doc.getRootElement();
			if (!root.getName().equals("root")) {
				logger.error("读取root根节点异常");
			}
			if (root != null) {
				for (Iterator i = root.elementIterator(); i.hasNext();) {
					ToolBean bean = new ToolBean();
					Element toolBar = (Element) i.next();
					List<ToolComponentBean> componentList = new ArrayList<ToolComponentBean>();
					if (toolBar.getName().equals("toolBar")) {
						bean.setName(toolBar.attributeValue("name"));
						bean.setI18nName(toolBar.attributeValue("i18nName"));
						bean.setInitIndex(Integer.parseInt(toolBar
								.attributeValue("initIndex")));
						for (Iterator j = toolBar.elementIterator(); j
								.hasNext();) {
							Element component = (Element) j.next();
							if (component.getName().equals("component")) {// 工具按钮
								ToolComponentBean componentBean = new ToolComponentBean();
								componentBean.setName(component
										.attributeValue("name"));
								componentBean.setClassName(component
										.attributeValue("className"));
								componentBean.setCompEnabled(!"false"
										.equals(component
												.attributeValue("isEnabled")));
								componentList.add(componentBean);
							} else if (component.getName().equals("separator")) {// 分割符
								ToolComponentBean componentBean = new ToolComponentBean();
								componentList.add(componentBean);
							}

						}
						bean.setToolList(componentList);
					}

					toolConf.put(bean.getName(), bean);
				}
			}

		} catch (DocumentException e) {
			logger.error(path + "解析失败严重错误" + path);
		}
	}

	/**
	 * 根据用户权限加载
	 * */
	public static void initConf(String path, List<String> privilege) {
		try {
			SAXReader reader = new SAXReader();
			Document doc = reader.read(path);
			Element root = (Element) doc.getRootElement();
			if (!root.getName().equals("root")) {
				logger.error("读取root根节点异常");
			}
			if (root != null) {
				for (Iterator i = root.elementIterator(); i.hasNext();) {
					ToolBean bean = new ToolBean();
					Element toolBar = (Element) i.next();
					List<ToolComponentBean> componentList = new ArrayList<ToolComponentBean>();
					if (toolBar.getName().equals("toolBar")) {
						bean.setName(toolBar.attributeValue("name"));
						bean.setI18nName(toolBar.attributeValue("i18nName"));
						bean.setInitIndex(Integer.parseInt(toolBar
								.attributeValue("initIndex")));
						for (Iterator j = toolBar.elementIterator(); j
								.hasNext();) {
							Element component = (Element) j.next();
							if (component.getName().equals("component")) {// 工具按钮
								ToolComponentBean componentBean = new ToolComponentBean();
								componentBean.setName(component
										.attributeValue("name"));
								componentBean.setClassName(component
										.attributeValue("className"));
								componentBean.setCompEnabled(!"false"
										.equals(component
												.attributeValue("isEnabled")));
								componentBean.setPrivilegestr(component
										.attributeValue("privilegestr"));
								if(haveAuthority(componentBean,privilege)){
									componentList.add(componentBean);
								}
							} else if (component.getName().equals("separator")) {// 分割符
								ToolComponentBean componentBean = new ToolComponentBean();
								componentList.add(componentBean);
							}

						}
						bean.setToolList(componentList);
					}

					toolConf.put(bean.getName(), bean);
				}
			}

		} catch (DocumentException e) {
			logger.error(path + "解析失败严重错误" + path);
		}
	}

	private static boolean haveAuthority(ToolComponentBean cb, List<String> privilege) {
		return cb.getPrivilegestr() == null
				|| cb.getPrivilegestr().trim().equals("")
				|| privilege.contains(cb.getPrivilegestr());
	}

	public static void main(String[] args) {
		initConf("conf/default/tool.xml");
		Set<String> keySet = toolConf.keySet();
		Iterator<String> it = keySet.iterator();
		while (it.hasNext()) {
			String key = it.next();
			ToolBean value = toolConf.get(key);
			System.out.println("name:" + value.getName());
			System.out.println("i18nName" + value.getI18nName());
			List<ToolComponentBean> a = new ArrayList<ToolComponentBean>();
			a = value.getToolList();
			if (a != null && a.size() != 0) {
				for (int i = 0; i < a.size(); i++) {
					ToolComponentBean comp = new ToolComponentBean();
					comp = a.get(i);
					System.out.println("componentName:" + comp.getName());
					System.out.println("componentClassName:"
							+ comp.getClassName());
				}
			}
		}
	}

	public static Map<String, ToolBean> getToolConf() {
		return toolConf;
	}

}
